$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'activationcode/list',
        datatype: "json",
        colModel: [			
			{ label: 'codeId', name: 'codeId',hidden: true,  index: 'code_id', width: 50, key: true },
			{ label: '激活码', name: 'codeNo', index: 'code_no', width: 200 },
			{ label: '过期时间', name: 'expires', index: 'expires', width: 100 , formatter: function(value, options, row){
                if (value ==="1") return "10分钟（试用）";
                if (value==="2") return "6个月";
                if (value==="3") return "12个月";
            } },
			{ label: '所属脚本', name: 'scriptName', index: 'scriptName', width: 100 },
			{ label: '分配用户', name: 'userName', index: 'userName', width: 100 },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 150 },
			{ label: '状态', name: 'status', index: 'status', width: 50, formatter: function(value, options, row){

                if (value === 1) return '<span class="label label-danger">禁用</span>';
                if (value===  0) return '<span class="label label-success">未使用</span>';
                if (value===  2) return '<span class="label label-warning">使用中</span>';
                if (value=== -1) return '<span class="label label-danger">已过期</span>';
            } }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });

});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            code: null,
            userName:null
        },
		showList: true,
		title: null,
		activationCode: {},
        scriptList:{},
        expiresList:{},
		userList:{},
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.activationCode = {userId:"",expiresType:"",scriptArray:[""]};
            vm.scriptList = {};
            //获取脚本信息
            this.getScriptList();
            //获取有效期配置
			this.getExpiresType();
			//获取用户
			this.getUsers();

		},
		update: function (event) {
			var codeId = getSelectedRow();
			if(codeId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(codeId)
		},
		saveOrUpdate: function (event) {
			var url = vm.activationCode.codeId == null ? "activationcode/save" : "activationcode/update";
			console.log(JSON.stringify(vm.activationCode));
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.activationCode),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var codeIds = getSelectedRows();
			if(codeIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "activationcode/delete",
                    contentType: "application/json",
				    data: JSON.stringify(codeIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},

        stop :function (event) {

            var codeIds = getSelectedRows();
            if(codeIds == null){
                return ;
            }

            confirm('确定要禁用选中的激活码？', function(){
                $.ajax({
                           type: "POST",
                           url: baseURL + "activationcode/stop",
                           contentType: "application/json",
                           data: JSON.stringify(codeIds),
                           success: function(r){
                               if(r.code == 0){
                                   alert('操作成功', function(index){
                                       $("#jqGrid").trigger("reloadGrid");
                                   });
                               }else{
                                   alert(r.msg);
                               }
                           }
                       });
            });

        }
		,
		getInfo: function(codeId){
			$.get(baseURL + "activationcode/info/"+codeId, function(r){
                vm.activationCode = r.activationCode;
            });
		},
        getScriptList: function(){
            $.get(baseURL + "activationcode/select", function(r){
                vm.scriptList = r.list;
            });
        },
		getExpiresType:function () {//查询有效期
            $.get(baseURL + "activationcode/selectExpires", function(r){
                vm.expiresList = r.list;
            });
        },
		getUsers:function () {
            $.get(baseURL + "activationcode/selectUsers", function(r){
                vm.userList = r.list;
            });

        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'code': vm.q.code,'userName':vm.q.userName},
                page:page
            }).trigger("reloadGrid");
		}
	}
});